Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • fix input/output token count in trace spans

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Dec 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Dec 3, 2025 4:31am

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 3, 2025

Greptile Overview

Greptile Summary

Fixed token count aggregation in calculateCostSummary to handle both naming conventions (prompt/input and completion/output) when extracting token usage from trace spans.

Key changes:

  • Added fallback logic using nullish coalescing to check both span.tokens.prompt and span.tokens.input for prompt tokens
  • Added fallback logic for span.tokens.completion and span.tokens.output for completion tokens
  • Extracted token values into variables to ensure consistency across both total and per-model aggregation
  • Removed outdated comments that no longer applied
  • Aligns with existing pattern used in telemetry.ts

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is a straightforward bug fix that adds defensive fallback logic for token counting. It aligns with existing patterns in the codebase (telemetry.ts), has no breaking changes, and only improves accuracy of token aggregation without affecting other functionality
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
apps/sim/lib/logs/execution/logging-factory.ts 5/5 Added fallback logic for token count fields to handle both prompt/input and completion/output naming conventions, ensuring accurate token aggregation across different trace span formats

Sequence Diagram

sequenceDiagram
    participant Caller
    participant calculateCostSummary
    participant collectCostSpans
    participant TraceSpans
    
    Caller->>calculateCostSummary: Call with traceSpans[]
    calculateCostSummary->>calculateCostSummary: Initialize totals & models object
    calculateCostSummary->>collectCostSpans: Recursively collect spans with cost data
    collectCostSpans->>TraceSpans: Traverse span tree
    TraceSpans-->>collectCostSpans: Return spans with cost info
    collectCostSpans-->>calculateCostSummary: Return costSpans[]
    
    loop For each costSpan
        calculateCostSummary->>calculateCostSummary: Extract promptTokens (prompt ?? input)
        calculateCostSummary->>calculateCostSummary: Extract completionTokens (completion ?? output)
        calculateCostSummary->>calculateCostSummary: Aggregate costs and tokens
        calculateCostSummary->>calculateCostSummary: Update model-specific metrics
    end
    
    calculateCostSummary->>calculateCostSummary: Add BASE_EXECUTION_CHARGE
    calculateCostSummary-->>Caller: Return cost summary with totals
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1 waleedlatif1 merged commit 3e83fb3 into staging Dec 3, 2025
9 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/trace-spans branch December 3, 2025 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants